/*
 * @lc app=leetcode.cn id=122 lang=javascript
 *
 * [122] 买卖股票的最佳时机 II
 */

// @lc code=start
/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function (prices) {
  const n = prices.length;
  // 第 i 天交易完后手里有无股票的最大利润
  const profits = Array.from({ length: n }, () => new Array(2).fill(0));
  (profits[0][0] = 0), (profits[0][1] = -prices[0]);
  for (let i = 1; i < n; i++) {
    profits[i][0] = Math.max(profits[i - 1][0], profits[i - 1][1] + prices[i]);
    profits[i][1] = Math.max(profits[i - 1][1], profits[i - 1][0] - prices[i]);
  }
  return profits[n - 1][0];
};
// @lc code=end
